// 引入接口文件
import {ManualList,ManualDetail,cancelManual,confimManual,confirmChange} from './manualAuthenticationServ'

// 弹出消息框
import { message } from 'antd'

/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import config from 'config/config';
import { download } from 'utils/util'



const queryList = [
  // {name: '姓名', type: 'text', key: 'name', value: '',placeholder: '只支持全名搜索',},
  {name: '手机号码', type: 'text', key: 'mobile', value: '',placeholder: '只支持全号搜索',},
  {name: '证件类型', type: 'select', key: 'idCardType', value: '1',placeholder: '', notNeedAll: true,
  list:[
    {id: '1', name: '身份证'},
    {id: '2', name: '台胞证'},
    {id: '3', name: '回乡证'},
    {id: '9', name: '其他'},
  ]
},
  {name: '证件号码', type: 'text', key: 'idCardNo', value: '',placeholder: '只支持全号搜索',},
  // {
  //   name: '认证状态', type: 'select', key: 'status', value: '', notNeedAll: true,placeholder: '', list: [//已认证  未认证  全部
  //     {id: '', name: '全部'},
  //     {id: '10', name: '已认证'},
  //     {id: '30', name: '未认证'},
  //   ]
  // },

  
]
//传参的参数
function getQueryParmas(Array){
  let parmas = {}
  Array.map((item,idx)=>{
    parmas[item.key] = item.value
  })
  if(parmas.idCardNo.length>0 && parmas.idCardType.length<1){
    message.error('请选择证件类型')
  }
  return parmas
}

// 初始默认状态
const defultState = {
  loading: false,
  btnLoading: false,
  exportLoading: false,
  queryList,
  statisticsInfo: {},//统计信息
  currentItem: {},
  modalObj:{
    visible:false,
  },
  productSelf: [],
  activeKey: '1',
  // 表格数据
  tableData: {
    dataSource: [
    ],
    total: 0,
  },
  // 表格分页设置
  pageConfig: {
    pageSize: Number.parseInt(config.smallPageSizeList[0]),
    pageNo: 1
  },
  teamList:[],
  // 弹出框表格数据
  tableModalData: {
    dataSource: [
    ],
    // total: 0,
  },
  query:[]
}

// 声明module
const tmpModule = {
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({ type: 'updateStore', payload: {...cloneDeep(defultState), ...query} });//每次进去页面先重置状态
        } 
      })
    },
  },

  // 方法列表
  effects: {
    
    *pageChange({ payload }, { put, select }) {
      const { pageNo, pageSize } = payload;
      const { pageConfig } =yield select(state => state[tmpModule.namespace])
      yield put({ type: 'updateStore', payload: { pageConfig: { ...pageConfig, ...payload } } });
      yield put({ type: 'search', payload: { pageNo, pageSize } });
    },
    // 查询条件重置
    *queryReset({ }, { put }) {
      yield put({ type: 'updateStore', payload: { queryList: cloneDeep(queryList) } });
      yield put({ type: 'getManualList', payload: { ...getQueryParmas(cloneDeep(queryList)) } });
      // yield put({ type: 'search' })
    },
    *search({},{put,select,call}){
      console.log(333)
      const {queryList } = yield select(state =>state[tmpModule.namespace])
      console.log(...getQueryParmas(queryList))

      yield put({type:"getManualList",payload:{...getQueryParmas(queryList)}})
    },
    //改变查询条件
    *inputChange({ payload }, { call, put, select }) {
      let { queryList } = yield select(state => state[tmpModule.namespace])
      const { key, value } = payload;
      let data = queryList.find(item => item.key === key);
      data.value = value;
      yield put({ type: 'updateStore', payload: { queryList: [...queryList, ...data] } });
    },
    //获取列表
    *getManualList({payload},{call,put,select}){
      let {tableData,pageConfig} = yield select(state => state[tmpModule.namespace])
      let {success, msg , data} = yield call(ManualList, {...payload})
      if(success){
        tableData.dataSource = data
        yield put ({type:'updateStore',payload:{tableData,pageConfig}})
      }
    },
    //获取详情
    *getManualDetail({payload},{call,put,select}){
      let {tableModalData,query} = yield select (state => state[tmpModule.namespace])
      let {success,data,msg} = yield call(ManualDetail,{...payload})
      if(success){
        tableModalData.dataSource = [].concat(data)
        tableModalData.total = 1
        query=data
        yield put ({type:'updateStore',payload:{tableModalData,query}})
      }
    },
    //保存修改
    *confirmChange({payload},{call,put,select}){
      let {query} = yield select (state => state[tmpModule.namespace])
      console.log(query)
      let {success} = yield call(confirmChange,{...query})
      if(success){
          message.success('保存成功')
          yield put ({type:'search',payload:{}})
      }
    },
    //确定认证
    *confimManual({payload},{call,put,select}){
      // let {tableModalData} = yield select (state => state[tmpModule.namespace])
      console.log(payload)
      let {success,rows,msg,total} = yield call(confimManual,{...payload})
      if(success){
        yield put({type:"search",payload:{}})//刷新用户查找的那一条
      }
    },
    //取消认证
    *cancelManual({payload},{call,put,select}){
      //let {tableModalData} = yield select (state => state[tmpModule.namespace])
      console.log(payload)
      let {success,rows,msg,total} = yield call(cancelManual,{...payload})
      if(success){
        yield put({type:"search",payload:{}})//刷新用户查找的那一条
      }
    }
  },

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
